package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyXinHongYuXin;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeXinHongYuXin;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 * 成都兴宏誉信-信息流房/公积金-50API
 * @ Author 江小游
 * @ Date 2025/5/13 11:01
 */
@ApiPlan(productId = "192", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5XinHongYuXin implements ApiOrgPhoneMd5ConvertHandler {

    private static final String PROMOTION = "ZZ04";
    private static final Integer FILED_ID=48;
    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        OrgDataExchangeXinHongYuXin orgDataExchangeXinHongYuXin = new OrgDataExchangeXinHongYuXin();
        orgDataExchangeXinHongYuXin.setMobile(apiDataExchangeParameter.getPhoneMd5());
        return orgDataExchangeXinHongYuXin;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        apiApplyParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        OrgApplyXinHongYuXin orgApplyXinHongYuXin = new OrgApplyXinHongYuXin();
        //拓展
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //file_id
        orgApplyXinHongYuXin.setFile_id(FILED_ID);
        //promotion
        orgApplyXinHongYuXin.setPromotion(PROMOTION);
        //姓名
        orgApplyXinHongYuXin.setName(apiApplyParameter.getUserName());
        //手机号码
        orgApplyXinHongYuXin.setMobile(apiApplyParameter.getPhone());
        //年龄(若没有传0)
        orgApplyXinHongYuXin.setAge(apiFieldConverter.getAgeInt());
        //性别(不传默认0。男传0、女传1)
        if (ValidParam.isEmpty(apiApplyParameter.getSex())) {
            Integer newSex = ChooseEq.create()
                    .when(apiApplyParameter.getSex(), "0", 0)
                    .when(apiApplyParameter.getSex(), "1", 1)
                    .end();
            orgApplyXinHongYuXin.setSex(newSex);
        }
        //城市(如天津市)
        orgApplyXinHongYuXin.setCity(apiApplyParameter.getCity());
        //是否有房(是传1,否传0)
        orgApplyXinHongYuXin.setIs_house(apiFieldConverter.hasRealEstateInfoToNumber());
        //是否有车(是传1,否传0)
        orgApplyXinHongYuXin.setIs_car(apiFieldConverter.hasCarInfoToNumber());
        //是否有公司营业执照(是传1,否传0)
        orgApplyXinHongYuXin.setIs_company(apiFieldConverter.hasBizLicenseYearToNumber());
        //是否有信用卡 (是传1,否传0)
        orgApplyXinHongYuXin.setIs_credit(apiFieldConverter.hasCreditLimitToNumber());
        //是否有保单(是传1,否传0)
        orgApplyXinHongYuXin.setIs_insurance(apiFieldConverter.hasInsurancePolicyToNumber());
        //是否有社保(是传1,否传0)
        orgApplyXinHongYuXin.setIs_social(apiFieldConverter.hasSocialSecurityToNumber());
        //是否有公积金(是传1,否传0)
        orgApplyXinHongYuXin.setIs_fund(apiFieldConverter.hasProvidentFundToNumber());
        //是否有打卡工资(是传1,否传0)
        if (ValidParam.isEmpty(apiApplyParameter.getSalaryPayMethod())) {
            Integer newWork = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                    .when(apiApplyParameter.getSalaryPayMethod(), "2", 0)
                    .when(apiApplyParameter.getSalaryPayMethod(), "3", 0)
                    .end();
            orgApplyXinHongYuXin.setIs_work(newWork);
        }
        //是否有营业税(是传1,否传0)
        orgApplyXinHongYuXin.setIs_tax(0);
        //是否有微粒贷(是传1,否传0)
        orgApplyXinHongYuXin.setWebank(0);
        //申请额度（单位:元）
        orgApplyXinHongYuXin.setMoney_demand(apiFieldConverter.getLoanAmountUnit());
        //备注
        orgApplyXinHongYuXin.setContent(apiApplyParameter.getFormRemark());
        return orgApplyXinHongYuXin;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
